<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Position extends HYD_Controller {
    private $_search_field_arr  = array();
    
    public function __construct(){
        parent::__construct();
        $this->load->model('position/position_model');
        $this->field_lists_init();
    }
    
    private function field_lists_init(){  
        //搜索中的“查询条件”选单                 
        $this->_search_field_arr = array('name'=> '商品名称',
                                         'description'=> '商品描述',
                                         'price'=> '商品价格');
    }
    
    public function list_position(){
        //$module = $this->input->get('module');
        //$action = $this->input->get('action');
        //log_message('error', $module.'-'.$action.'-begin index-'.microtime());
        $sql_str = '';
        $start = $this->input->get_post('start') ? $this->input->get_post('start') : 0;
        $limit = $this->input->get_post('limit') ? $this->input->get_post('limit') : MAX_DISPLAY_SEARCH_RESULTS;
        $search_type = $this->input->get_post('search_type');
        if($search_type == 'button'){
            $search_content = $this->input->get_post('search_content');
            $sql_str .= 'classid='.$search_content;
        }elseif($search_type == 'text'){
            $search_content = $this->input->get_post('search_content');
            if (strlen(trim($search_content)) > 0) {
                $value = addslashes($search_content);
                $search_field_arr = array_keys($this->_search_field_arr);
                foreach ($search_field_arr as $field) {
                    if ($sql_str){
                        $sql_str .= ' OR ';
                    }
                    $sql_str .= "{$field} like '%{$value}%'";
                }
            }
        }
        
        $position = $this->position_model->get_lists($start, $limit, $sql_str);     
        $response = array(EXT_JSON_READER_TOTAL => $this->position_model->get_total(),
                          EXT_JSON_READER_ROOT => $position);
        
        $this->output->set_content_type('application/json')->set_output(json_encode($response));
        //return $response;
	}
    
    public function save_position(){
        //$this->load->helper('form');
        $this->form_validation->set_rules('name', '团队名称', 'required');
        $this->form_validation->set_rules('description', '岗位描述', 'required');
        
        $id = $this->input->get_post('id');
        if ($this->form_validation->run() === FALSE){//对以上验证做判断
            $response = array('success' => FALSE, 'feedback' => '提交信息不合法!');
        }else{
            $post_arr = array();
            $post_arr['id'] = $id;
            $post_arr['name'] = $this->input->get_post('name');
            $post_arr['description'] = $this->input->get_post('description');
            $post_arr['module'] = json_decode($this->input->post('position_module'), TRUE);

            $res = $this->position_model->save($post_arr['id'], $post_arr);//edit时提交的id完全对应已经在model进行了指定
            switch ($res) {
                case 1:
                    $response = array('success' => TRUE, 'feedback' => '保存成功');
                    break;
                case -3:
                    $response = array('success' => FALSE, 'feedback' => '岗位已经存在');
                    break;            
                case MODEL_RETURN_DBERROR:
                    $response = array('success' => FALSE, 'feedback' => '数据库错误');
                    break;
            }   
        }
       
        return $response;
    }
    
    //岗位权限列表
    public function get_position_module() {       
        $id = 0;
        if ($this->input->get_post('id') != '') {
            $id = $this->input->get_post('id');
        }
        $res = $this->position_model->get_module_ext_json_tree($id);
        return $res;        
    } 
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */